// 文件路径: src/main/java/com/chixing/controller/ScheduleController.java

package com.chixing.controller;

import com.chixing.entity.Schedule;
import com.chixing.service.IScheduleService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private IScheduleService scheduleService;

    /**
     * 获取所有场次
     * @return 场次列表
     */
    @GetMapping("")
    public Result getAllSchedules() {
        try {
            List<Schedule> schedules = scheduleService.getAllSchedules();
            return Result.getSuccess(schedules);
        } catch (Exception e) {
            return Result.error("获取场次列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取场次详情
     * @param scheduleId 场次ID
     * @return 场次信息
     */
    @GetMapping("/{scheduleId}")
    public Result getSchedule(@PathVariable Integer scheduleId) {
        try {
            Schedule schedule = scheduleService.getScheduleById(scheduleId);
            if (schedule != null) {
                return Result.getSuccess(schedule);
            } else {
                return Result.error("未找到指定的场次");
            }
        } catch (Exception e) {
            return Result.error("获取场次详情失败: " + e.getMessage());
        }
    }

    /**
     * 根据电影ID获取场次
     * @param movieId 电影ID
     * @return 场次列表
     */
    @GetMapping("/movie/{movieId}")
    public Result getSchedulesByMovieId(@PathVariable Integer movieId) {
        try {
            List<Schedule> schedules = scheduleService.getSchedulesByMovieId(movieId);
            return Result.getSuccess(schedules);
        } catch (Exception e) {
            return Result.error("获取电影场次失败: " + e.getMessage());
        }
    }


    /**
     * 添加场次
     * @param schedule 场次对象
     * @return 添加结果
     */
    @PostMapping("")
    public Result addSchedule(@RequestBody Schedule schedule) {
        try {
            schedule.setCreateTime(LocalDateTime.now());
            schedule.setUpdateTime(LocalDateTime.now());
            boolean result = scheduleService.addSchedule(schedule);
            if (result) {
                return Result.saveSuccess("添加场次成功");
            } else {
                return Result.error("添加场次失败");
            }
        } catch (Exception e) {
            return Result.error("添加场次异常: " + e.getMessage());
        }
    }

    /**
     * 更新场次
     * @param scheduleId 场次ID
     * @param schedule 场次对象
     * @return 更新结果
     */
    @PutMapping("/{scheduleId}")
    public Result updateSchedule(@PathVariable Integer scheduleId, @RequestBody Schedule schedule) {
        try {
            schedule.setScheduleId(scheduleId);
            schedule.setUpdateTime(LocalDateTime.now());
            boolean result = scheduleService.updateSchedule(schedule);
            if (result) {
                return Result.updateSuccess("更新场次成功");
            } else {
                return Result.error("更新场次失败");
            }
        } catch (Exception e) {
            return Result.error("更新场次异常: " + e.getMessage());
        }
    }

    /**
     * 删除场次
     * @param scheduleId 场次ID
     * @return 删除结果
     */
    @DeleteMapping("/{scheduleId}")
    public Result deleteSchedule(@PathVariable Integer scheduleId) {
        try {
            boolean result = scheduleService.deleteSchedule(scheduleId);
            if (result) {
                return Result.deleteSuccess("删除场次成功");
            } else {
                return Result.error("删除场次失败");
            }
        } catch (Exception e) {
            return Result.error("删除场次异常: " + e.getMessage());
        }
    }
}
